package io.sage.modules.sys.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;

import io.sage.common.utils.Constant;
import io.sage.common.utils.MapUtils;
import io.sage.modules.sys.dao.SysMenuDao;
import io.sage.modules.sys.entity.SysMenuEntity;
import io.sage.modules.sys.service.SysMenuService;
import io.sage.modules.sys.service.SysRoleMenuService;
import io.sage.modules.sys.service.SysUserService;

@Service("sysMenuService")
public class SysMenuServiceImpl extends ServiceImpl<SysMenuDao, SysMenuEntity> implements SysMenuService {
	@Autowired
	private SysUserService sysUserService;
	@Autowired
	private SysRoleMenuService sysRoleMenuService;

	@Override
	public List<SysMenuEntity> queryListParentId(Long parentId, List<Long> menuIdList, Integer sysManageId) {
		List<SysMenuEntity> menuList = queryListParentId(parentId);
		if(menuIdList == null){
			return menuList;
		}

		List<SysMenuEntity> userMenuList = new ArrayList<>();
		for(SysMenuEntity menu : menuList){
			if(menuIdList.contains(menu.getMenuId())){
				//如果 sysManageId 不为空，检测对应菜单项是否包含 sysManageId，如果不包含就不添加进结果集合。
				if(sysManageId!=null) {
					String sysManageId1 = menu.getSysManageId();
					List<String> sysManageIdStrList = Arrays.asList(sysManageId1==null?new String[]{}:sysManageId1.split(","));
					if(!sysManageIdStrList.contains(sysManageId+""))
						continue;
				}
				userMenuList.add(menu);
			}
		}
		return userMenuList;
	}

	@Override
	public List<SysMenuEntity> queryListParentId(Long parentId) {
		return baseMapper.queryListParentId(parentId);
	}

	@Override
	public List<SysMenuEntity> queryNotButtonList() {
		return baseMapper.queryNotButtonList();
	}

	@Override
	public List<SysMenuEntity> getUserMenuList(Long userId, Integer sysManageId) {
		//系统管理员，拥有最高权限
//		if(userId == Constant.SUPER_ADMIN){
//			return getAllMenuList(null, sysManageId);
//		}

		//用户菜单列表
		List<Long> menuIdList = sysUserService.queryAllMenuId(userId);
		return getAllMenuList(menuIdList, sysManageId);
	}

	@Override
	public void delete(Long menuId){
		//删除菜单
		this.deleteById(menuId);
		//删除菜单与角色关联
		sysRoleMenuService.deleteByMap(new MapUtils().put("menu_id", menuId));
	}

	/**
	 * 获取所有菜单列表
	 */
	private List<SysMenuEntity> getAllMenuList(List<Long> menuIdList, Integer sysManageId){
		//查询根菜单列表
		List<SysMenuEntity> menuList = queryListParentId(0L, menuIdList, sysManageId);
		//递归获取子菜单
		getMenuTreeList(menuList, menuIdList, sysManageId);

		return menuList;
	}

	/**
	 * 递归
	 */
	private List<SysMenuEntity> getMenuTreeList(List<SysMenuEntity> menuList, List<Long> menuIdList, Integer sysManageId){
		List<SysMenuEntity> subMenuList = new ArrayList<SysMenuEntity>();

		for(SysMenuEntity entity : menuList){
			//如果类型不是按钮且sysManageId不为空，检测对应菜单项的sysManageId列表是否包含sysManageId
			if(sysManageId!=null && entity.getType()!=Constant.MenuType.BUTTON.getValue()) {
				String sysManageId1 = entity.getSysManageId();
				List<String> sysManageIdStrList = Arrays.asList(sysManageId1==null?new String[]{}:sysManageId1.split(","));
				System.out.println("sysManageIdStrList:::"+sysManageIdStrList);
				//如果对应菜单项不包含 sysManageId ，就不添加进结果集合，也不递归查找子菜单项。
				if(!sysManageIdStrList.contains(sysManageId+""))
					continue;
			}
			//目录
			if(entity.getType() == Constant.MenuType.CATALOG.getValue()){
				entity.setList(getMenuTreeList(queryListParentId(entity.getMenuId(), menuIdList, sysManageId), menuIdList, sysManageId));
			}
			subMenuList.add(entity);
		}

		return subMenuList;
	}
}
